import { Component, OnInit } from '@angular/core';
import { User } from '../../domain/user';
import { ManagerService } from '../../service/manager.service';
import { DepartmentManagerDeal } from '../../domain/department-manager-deal';
import { DeputyManagerDeal } from '../../domain/deputy-manager-deal';
import { MainManagerDeal } from '../../domain/main-manager-deal';
import { LeaveRequest } from '../../domain/leave-request';
import { OutRequest } from '../../domain/out-request';
import { AuthService } from '../../service/auth.service';
import { initDomAdapter } from '@angular/platform-browser/src/browser';
/*

@Author:李朝晖

@Date: 2018-12-12

@经理查看请假功能实现

@Last Modified time: 2018-12-16
*/
@Component({
  selector: 'app-manager-look-leave',
  templateUrl: './manager-look-leave.component.html',
  styleUrls: ['./manager-look-leave.component.css']
})
export class ManagerLookLeaveComponent implements OnInit {

  private manager: User;
  private users: User;
  private user: User;
  private leave: LeaveRequest;
  private out: OutRequest;

  private departmentManagerDeal: DepartmentManagerDeal[];
  private deputyManagerDeal: DeputyManagerDeal[];
  private mainManagerDeal: MainManagerDeal[];

  private departmentDeal: DepartmentManagerDeal;
  private deputyDeal: DeputyManagerDeal;
  private mainDeal: MainManagerDeal;

  private detail = null;


  constructor(private managerService: ManagerService) {
    this.manager = {
      id: 2,
      user_name: "default",
      name: "default",
      age: null,
      sex: null,
      department: "软件工程", //部门
      position: "部门经理", //职务
      annual_leave_length: null, //年假上限
      remain_annual_leave: null, //剩余年假
    };
    this.init();
  }

  init() {
    this.user = {
      id: null,
      user_name: null,
      name: null,
      age: null,
      sex: null,
      department: null,
      position: null,
      annual_leave_length: null,
      remain_annual_leave: null,
    }
    this.out = {
      id: null, 
      user_id: null,
      start_time: null,
      end_time: null,
      reason: null,
      state: null,
      duration: null,
      suggestion: null,
    }
    this.leave = {
      id: null, 
      user_id: null,
      start_time: null,
      end_time: null,
      reason: null,
      state: null,
      duration: null,
      type: null,
      suggestion: null,
    }
  }

  ngOnInit() {
    this.getUser();
  }

  getUser() {
    var id = AuthService.getUserId();
    if (id == 0) {
      alert("请先登录");
    } else {
      this.managerService.getUser(id)
        .subscribe(manager => {
          this.manager = manager[0];
          this.getManagerDeal();
        });
    }
  }

  getManagerDeal() {
    if (this.manager.position == "部门经理") {
      this.managerService.getDepartmentManagerDeal(this.manager.id)
        .subscribe(deal => {
          this.departmentManagerDeal = deal;
          //按时间排序
          for (var i = 0; i < this.departmentManagerDeal.length - 1; i++) {
            if (this.departmentManagerDeal[i].approval_time < this.departmentManagerDeal[i + 1].approval_time) {
              var temp: DepartmentManagerDeal = this.departmentManagerDeal[i];
              this.departmentManagerDeal[i] = this.departmentManagerDeal[i + 1];
              this.departmentManagerDeal[i + 1] = temp;
              i = 0;
            }
          }
        });
    } else if (this.manager.position == "副总经理") {
      this.managerService.getDeputyManagerDeal(this.manager.id)
        .subscribe(deal => {
          this.deputyManagerDeal = deal;
          //按时间排序
          for (var i = 0; i < this.deputyManagerDeal.length - 1; i++) {
            if (this.deputyManagerDeal[i].approval_time < this.deputyManagerDeal[i + 1].approval_time) {
              var temp: DeputyManagerDeal = this.deputyManagerDeal[i];
              this.deputyManagerDeal[i] = this.deputyManagerDeal[i + 1];
              this.deputyManagerDeal[i + 1] = temp;
              i = 0;
            }
          }
        });
    } else if (this.manager.position == "总经理") {
      this.managerService.getMainManagerDeal(this.manager.id)
        .subscribe(deal => {
          this.mainManagerDeal = deal;
          //按时间排序
          for (var i = 0; i < this.mainManagerDeal.length - 1; i++) {
            if (this.mainManagerDeal[i].approval_time < this.mainManagerDeal[i + 1].approval_time) {
              var temp: MainManagerDeal = this.mainManagerDeal[i];
              this.mainManagerDeal[i] = this.mainManagerDeal[i + 1];
              this.mainManagerDeal[i + 1] = temp;
              i = 0;
            }
          }
        });
    } else {
      alert("请先登录！");
    }
  }

  //依据request的id和type查询
  lookDetail(id: number, type: number) {
    if (type == 0) {//外出out
      this.detail = 1;
      this.managerService.getOutRequest(id)
        .subscribe(out => {
          this.out = out[0];
          this.managerService.getUser(this.out.user_id)
            .subscribe(user => {
              this.user = user[0];
            });
        });
    } else if (type == 1) {//请假leave
      this.detail = 1;
      this.managerService.getLeaveRequest(id)
        .subscribe(leave => {
          this.leave = leave[0];
          this.managerService.getUser(this.leave.user_id)
            .subscribe(user => {
              this.user = user[0];
            });
        });
    }
  }

  backDetail() {
    this.detail = null;
    this.init();
  }

  getState(num: number) {
    if (num == 0)
      return "拒绝请假";
    else if (num == 1)
      return "已通过";
    else if (num == 2)
      return "等待部门经理";
    else if (num == 3)
      return "等待副总经理";
    else if (num == 4)
      return "等待总经理";
    else
      return "";
  }
  getType(num: number) {
    if (num == 0)
      return "外出";
    else {
      return "请假";
    }
  }

}
